package cn.lili.modules.grab.xdxt.reps.order;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CreateOrderReps {

    private int code;
    private String msg;
    private Result result;
    private String traceId;

    @Data
    public static class Result {
        @ApiModelProperty("平台订单号")
        private String unionNo;
        @ApiModelProperty("商品总数")
        private int totalNum;
        @ApiModelProperty("商户平台订单号，商户订单唯一标识，要保证唯一")
        private String platformOrderNo;
        @ApiModelProperty("商品总额")
        private double expFeeAmount;
        @ApiModelProperty("运费总额")
        private double goodsMoneyAmount;
        @ApiModelProperty("支付总额")
        private double payMoney;
        @ApiModelProperty("子订单数组")
        private List<Order> orderList;

        @Data
        public static class Order {
            @ApiModelProperty("子订单id")
            private int suborderId;
            @ApiModelProperty("子单商品总金额")
            private double totalGoodsAmount;
            @ApiModelProperty("子单运费")
            private double freightAmount;
            @ApiModelProperty("子单总金额")
            private double totalAmount;

            @ApiModelProperty("订单商品信息")
            private List<OrderDetail> orderDetailList;

            @Data
            public static class OrderDetail {
                @ApiModelProperty("订单商品明细id")
                private int orderDetailId;
                @ApiModelProperty("规格编码")
                private String specCode;
                @ApiModelProperty("规格名称")
                private String specName;
                @ApiModelProperty("规格信息json")
                private String specInfo;
                @ApiModelProperty("数量")
                private int num;
                @ApiModelProperty("单价")
                private double unitPrice;
                @ApiModelProperty("商品总金额")
                private double totalAmount;
            }
        }
    }

    /**
     * 将JSON构建为ListGoodsReps
     *
     * @param jsonString 值
     */
    public static CreateOrderReps builderCreateOrderReps(JSONObject jsonString) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        CreateOrderReps createOrderReps = mapper.readValue(jsonString.toString(), CreateOrderReps.class);
        return createOrderReps;
    }
}
